package com.ruoyi.suggestion.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.suggestion.domain.vo.SuggestionVO;
import com.ruoyi.user.domain.User;
import com.ruoyi.wx.utils.FileDirectoryUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.suggestion.mapper.SuggestionMapper;
import com.ruoyi.suggestion.domain.Suggestion;
import com.ruoyi.suggestion.service.ISuggestionService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 【平台建议】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-02
 */
@Service
@Slf4j
public class SuggestionServiceImpl implements ISuggestionService 
{
    @Autowired
    private SuggestionMapper suggestionMapper;

    @Value("${ruoyi.image}")
    private String image;


    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SuggestionVO selectSuggestionById(Long id)
    {
        Suggestion suggestion = suggestionMapper.selectSuggestionById(id);
        String image1 = suggestion.getImage();
        SuggestionVO suggestionVO = new SuggestionVO();
        BeanUtils.copyProperties(suggestion, suggestionVO);
        if (StringUtils.isNotEmpty(image1)) {
            List<String> imageList = Arrays.asList(image1.split(","));
            suggestionVO.setImageList(imageList);
        }
        return suggestionVO;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param suggestion 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SuggestionVO> selectSuggestionList(Suggestion suggestion)
    {
        List<Suggestion> suggestions = suggestionMapper.selectSuggestionList(suggestion);
        List<SuggestionVO> suggestionVOS = new ArrayList<>();
        for (Suggestion suggestion1 : suggestions) {
            SuggestionVO suggestionVO = new SuggestionVO();
            BeanUtils.copyProperties(suggestion1, suggestionVO);
            String image1 = suggestion1.getImage();
            if (image1 != null) {
                List<String> imageList = Arrays.asList(image1.split(","));
                suggestionVO.setImageList(imageList);
            }
            suggestionVOS.add(suggestionVO);
        }
        return suggestionVOS;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param suggestion 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSuggestion(Suggestion suggestion)
    {
        suggestion.setCreateTime(DateUtils.getNowDate());
        return suggestionMapper.insertSuggestion(suggestion);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param suggestion 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSuggestion(Suggestion suggestion)
    {
        return suggestionMapper.updateSuggestion(suggestion);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSuggestionByIds(Long[] ids)
    {
        return suggestionMapper.deleteSuggestionByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSuggestionById(Long id)
    {
        return suggestionMapper.deleteSuggestionById(id);
    }

    @Override
    public String suggestionUpload(String userId, MultipartFile file, HttpServletRequest request) {
        String fileUrl;
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            fileUrl = FileUploadUtils.upload(filePath, file);

        } catch (IOException e) {
            log.error("头像上传失败", e);
            fileUrl = "上传失败: " + e.getMessage();
        }
        return fileUrl;
    }

}
